<?php

namespace app\controller\property\system;

use think\App;
use app\controller\property\Base;
use think\exception\ValidateException;
use app\validate\system\SystemRegionValidate;
use app\common\repositories\system\SystemRegionRepository;

class SystemRegion extends Base
{
    protected $repository;

    public function __construct(App $app, SystemRegionRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    public function list()
    {
        $level = (int)$this->request->param('level', 0) + 1;
        $pid = (int)$this->request->param('pid', 0);
        if ($this->request->isAjax()) {
            [$page, $limit] = $this->getPage();
            $res = $this->repository->getList($page, $limit, $level, $pid);
            return json()->data(['code' => 0, 'data' => $res['list'], 'count' => $res['count']]);
        } else {
            return $this->fetch('system/region/list', [
                'addAuth' => property_auth('propertySystemRegionAdd'),
                'editAuth' => property_auth('propertySystemRegionEdit'),
                'delAuth' => property_auth('propertySystemRegionDelete'),
                'level' => $level,
                'pid' => $pid
            ]);
        }
    }

    /**
     * 添加地区信息
     *
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'id' => '',
                'name' => '',
                'area_code' => '',
                'level' => '',
                'remark' => '',
                'longitude' => '',
                'latitude' => '',
                'pid' => ''
            ]);
            validate(SystemRegionValidate::class)->scene('add')->check($param);
            try {
                $res = $this->repository->addInfo($param, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                exception_log('添加地区信息失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $pid = (int)$this->request->param('pid');
            $level = (int)$this->request->param('level');
            $pidName = '';
            if ($pid > 0) {
                $pidName = $this->repository->getSearch([])
                    ->where('id', $pid)->value('name');
            }
            return $this->fetch('system/region/add', [
                'regionLevel' => $this->repository::REGION_LEVE,
                'pid' => $pid,
                'level' => $level,
                'pidName' => $pidName
            ]);
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('数据不存在');
        }
        if ($this->request->isPost()) {
            $param = $this->request->param([
                'name' => '',
                'area_code' => '',
                'id' => '',
                'level' => '',
                'longitude' => '',
                'latitude' => '',
                'pid' => ''
            ]);
            validate(SystemRegionValidate::class)->scene('edit')->check($param);
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('编辑成功');
                } else {
                    return $this->error('编辑失败');
                }
            } catch (\Exception $e) {
                exception_log('编辑地区信息失败', $e);
                return $this->error('网络错误');
            }
        } else {
            $pidName = '';
            if ($info['pid'] > 0) {
                $pidName = $this->repository->getSearch([])
                    ->where('id', $info['pid'])->value('name');
            }
            return $this->fetch('system/region/edit', [
                'regionLevel' => $this->repository::REGION_LEVE,
                'info' => $info,
                'pidName' => $pidName
            ]);
        }
    }

    public function delete()
    {
        $id = $this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('数据不存在');
        }
        if ($this->repository->hasChild($id)) {
            return $this->error('下级存在地区信息不能删除');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId);
        if ($res) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

}